package org.lgiavedoni.android.franq.command.system;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.lgiavedoni.android.franq.core.command.Command;
import org.lgiavedoni.android.franq.core.place.Place;
import org.lgiavedoni.android.franq.core.position.Position;
import org.lgiavedoni.android.franq.core.speak.CommonPhraseHelper;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;
import org.lgiavedoni.android.franq.service.phone.location.LocationService;
import org.lgiavedoni.android.franq.util.googe.MapsUtil;

public class LocationCommand extends Command {

	private static final String OPTION_MAP = "map";

	@Override
	public void setOptions(Options options) {
		options.addOption(OPTION_MAP, false, "Show the location on a Map Link");
	}

	@Override
	public boolean execute(CommandLine cmd) throws Exception {
		Position position = LocationService.inst.getLocation();

		if (cmd.getOptions() != null && cmd.getOptions().length == 1) {
			if (cmd.hasOption(OPTION_MAP)) {
				CommunicationService.inst.sendDefaultTextMessage("I am here: " + MapsUtil.getURL(position));
			}

		} else {
			if (position != null) {
				if (position instanceof Place && !((Place) position).isUnknowName())
					CommunicationService.inst.sendDefaultTextMessage("@" + ((Place) position).getName());
				else {
					CommunicationService.inst.sendDefaultTextMessage("I am here: " + position);
					// CommunicationService.inst.sendDefaultTextMessage("I am here: "
					// + MapsUtil.getURL(position));
				}
			} else
				CommunicationService.inst.sendMessage(CommonPhraseHelper.IDONT_KNOW);
		}
		return true;
	}

	@Override
	public String getCommandText() {
		return "location";
	}
}